package com.asset.dispoal.company.service.commom;

import com.asset.dispoal.company.domain.db.entity.SysCompanyDo;
import com.asset.dispoal.company.domain.db.mapper.SysCompanyMapper;
import com.asset.dispoal.company.dto.CompanyRequest;
import com.asset.dispoal.company.service.converter.CompanyConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.exception.BusinessException;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

/**
 * @author qr
 * @date 2022/1/25$
 */
@Component
public class UpdateCommand implements BaseCommand<CompanyRequest, Result> {

    @Autowired
    SysCompanyMapper sysCompanyMapper;

    @Override
    public Result execute(CompanyRequest request) {
        if (request == null || StringUtil.isBlank(request.getId())) {
            throw new BusinessException("参数错误");
        }
        short count = sysCompanyMapper.queryCompanyCodeExistExpect(request.getCode(), request.getType(), request.getId());
        if (count > 0) {
            throw new BusinessException(400, "公司编码重复！！", request.getCode());
        }
        final SysCompanyDo sysCompanyDo = CompanyConverter.INSTANCE.reqToDo(request);
        sysCompanyMapper.update(sysCompanyDo);
        return Result.ok();
    }
}
